(function($){
	var page = null;
 $.extend({
	pangination : function(options){
		var defaults = {
				//指定的显示字段
				feild:[],
				//数据展示块ID
				pageBody:"pageBody",
				//分页条
				pageFoot:"pageFoot",
				//数据请求
				url:"javascript:;",
				//操作按钮
				btn:[],
				//传入参数
				param : {},
				//数据
				data:{},
				//需要显示的字段
				specialCase:[]
			};
		var opts = $.extend({},defaults, options);
		var pageBody = $("#" + opts.pageBody);
		var pageFoot = $("#" + opts.pageFoot);
		pageBody.html("");
		pageFoot.html("");
		var isFeild = false;
		if(opts.feild && opts.feild.length > 0){
			isFeild = true;
		}
		//行对象
		var trObj;
		if(isFeild){
			//获取格式化数据
			var o = dataFormat(opts.url,opts.param,opts.feild);
			if(o==null){
				return null;
			}
			$.each(o,function(key,val){   
				trObj = $("<tr>");
				trObj.append("<input type='checkBox'>");
				$.each(val,function(n_k,n_v){
					if(n_v ==null){
						n_v="";
					}
					if(specialCase(n_k,opts.specialCase)){
						trObj.append("<td>"+n_v+"</td>");
					}else{
						
					}
				});
				//拼接按钮
				doBtn(opts.btn,trObj,pageBody,val);
			});
		}else{
			//获取数据
			var n = getData(opts.url,opts.param);
			if(n==null){
				return null;
			}
			$.each(n,function(k,v){
				trObj = $("<tr>");
				trObj.append("<td><input type='checkBox'></td>");
				$.each(v,function(l_k,l_v){
					if(l_v ==null){
						l_v="";
					}
					trObj.append("<td>"+l_v+"</td>");
				});
				//拼接按钮
				doBtn(opts.btn,trObj,pageBody,v);
			});
		}
		//拼接分页条
		pageFootInit(page, pageFoot, opts);
	}
	});
 	//按钮
 	function doBtn(btn,trObj,pageBody,val){
 			var buttons = btn || [];  
 			var butTd = $("<td>");
 			for(var indx in buttons){
 				var butObj = buttons[indx];
 				var buttonStr = $("<a href='#"+butObj.anchor+"' class='btn'>"+ (butObj.desc|| "" )+"</a>");
 				if( typeof butObj.func === 'function' ){
 					buttonStr.bind("click", val, butObj.func);
 				}
 				//关闭条件
 				if(butObj.close!=undefined){
 					//string:单个值匹配 int:最大值匹配 数组:存在匹配
 					$.each(butObj.close,function(k,v){
 						$.each(val,function(keyWord,value){
 							if(typeof v === 'string'){
 								if(k==keyWord &&v==value){
 									buttonStr.css("display","none");
 								}
 							}else if(typeof v === 'object'){
 								$.each(v,function(k_l,v_l){
 									if(keyWord==k_l &&value==v_l){
 										buttonStr.css("display","none");
 									}
 								});
 							}else if(typeof v === 'number'){
 								if(value===null){
 									buttonStr.css("display","none");
 								}else{
 									if(keyWord==k &&value>parseInt(v)){
 		 								buttonStr.css("display","none");
 		 							}
 								}
							}
 						});
 					});
 				}
 				butTd.append(buttonStr);
 			}
 			trObj.append(butTd);
 			pageBody.append(trObj);
 		
 	}
 	//发送请求获取数据
	function getData(url,param){
		this.url = url;
		this.param = param;
		//查询数据请求
		getDataVal = function(){
			var temp ={};
			$.ajax({
				url:this.url,
				data:this.param,
				type : "post",
				async:false,
				success:function(result){
					page = result.page;
					if(result.success){
						temp = result.data;
					}
				}
			});
			return temp;
		};
		return getDataVal();
	}
	//特例字段删除
	function specialCase(tempList,specialCaseLsit){
		for(var i =0;i<specialCaseLsit.length;i++){
			if(specialCaseLsit[i].name==tempList){
				return false;
			}
		}
		return true;
	}
	//数据格式处理
	function dataFormat(url,param,feild,trObj){
		var temp = getData(url,param);
		var tempList = [];
		if(temp==null){
			return null;
		}
		//去壳循环
		for(var x =0;x<temp.length;x++){
			var tempJson={};
			//取数据
			$.each(temp[x],function(k,v){
				
				//循环替换值
				for(var i = 0;i<feild.length;i++){
					if(feild[i].name == k){
						if(feild[i].type=="statuCode"){
							//状态码
							for(var j =0;j<feild[i].currData.length;j++){
								if(v === feild[i].currData[j]){
									v = feild[i].formatDate[j];
								}
							}
						}else if(feild[i].type=="date"){
							//日期格式
							var d = new Date(v);
							var tempDay = parseInt(d.getDate());
							var tempMonth = parseInt(d.getMonth()+1);
							d = d.getFullYear()+"-"+setOinTime(tempMonth)+"-"+setOinTime(tempDay);
							v = d;
						}else if(feild[i].type=="dateTime"){
							var d = new Date(v);
							var tempDay = parseInt(d.getDate());
							var tempMonth = parseInt(d.getMonth());
							tempMonth++;
							d = d.getFullYear()+"-"+setOinTime(tempMonth)+"-"+setOinTime(tempDay)+" "+setOinTime(d.getHours())+":"+setOinTime(d.getMinutes())+":"+setOinTime(d.getSeconds());
							v = d;
						}else if(feild[i].type=="dateError"){
							if(v != null){
								var d = new Date(v);
								var tempDay = parseInt(d.getDate());
								var tempMonth = parseInt(d.getMonth());
								tempMonth++;
								d = d.getFullYear()+"-"+setOinTime(tempMonth)+"-"+setOinTime(tempDay);
								v = d;	
							}
						}
						tempJson[k] = v;
					}
				}
			});
			tempList.push(tempJson);
		}
		return	tempList;
	}
	function setOinTime(temp){
		if(temp<10){
			return "0"+temp;
		}
		return temp;
	}
	//分页条处理
	function pageFootInit(_page, _pageFoot, _opts){
		
		var opts = _opts;
		var $pageFoot = _pageFoot;
		if(_page == null){
			var str = "<button class='btn'>首页</button>";
			str +="<button class='btn'>上一页</button>";
			str +="<button style='padding-bottom: 8px' class='btn current'>0</button>";
			str +="<button class='btn'>下一页</button>";
			str +="<button class='btn'>尾页</button>";
			$pageFoot.append(str);
		}else{
			var size = _page.size - 0;
			var index = _page.index  <= 0 ? 1  : _page.index;
	        var total =  _page.totalCount - 0;
			var pageCount = 0;
			
			if (total % size == 0) {
				pageCount = total / size;
			}else{
				pageCount =  Math.ceil(total / size);
			}
			 //添加第一页
			if (index <= 1)
				$pageFoot.append("<button class='disabled firstPage btn'>&lt;&nbsp;首页</button>");
			else {
				var first = $("<button class='btn' first='" + 1 + "'>&lt;&nbsp;首页</button>").click(function () {
					opts.param.index = 1;
					$.pangination(opts);
					return false;
				});
				$pageFoot.append(first);
			}
			//添加上一页
			if (index == 1){
				$pageFoot.append("<button class='disabled btn'>上一页</button>");
			}else {
				
				var pre = $("<button class='btn' pre='" + (index - 1) + "'>上一页</button>").click(function () {
					opts.param.index = index - 1;
					$.pangination(opts);
					return false;
				});
				$pageFoot.append(pre);
			}

			//设置分页的格式  这里可以根据需求完成自己想要的结果
			var interval = 1; //设置间隔
			var start = Math.max(1, index - interval); //设置起始页
			var end = Math.min(index + interval, pageCount);//设置末页

			if (index < interval + 1) {
				end = (2 * interval + 1) > pageCount ? pageCount : (2 * interval + 1);
			}

			if ((index + interval) > pageCount) {
				start = (pageCount - 2 * interval) < 1 ? 1 : (pageCount - 2 * interval);
			}

	                    //生成页码
	                    for (var j = start; j < end + 1; j++) {
	                        if (j == index) {
	                            var spanSelectd = $("<button style='padding-bottom: 8px' class='current btn'>" + j + "</button>");
	                            $pageFoot.append(spanSelectd);
	                        } //if 
	                        else {
	                            var a = $("<button style='padding-bottom: 8px' class='btn'>" + j + "</button>").click(function () {
	                            	var num = $(this).html() - 0;
	                            	opts.param.index = num;
	                            	$.pangination(opts);
	                                return false;
	                            });
	                            $pageFoot.append(a);
	                        } //else
	                    } //for

	                    //下一页
	                    if (index == end) {
	                        $pageFoot.append("<button class='disabled btn'>下一页</button>");

	                    }
	                    else {

	                        var next = $("<button class='btn' next='" + (index + 1) + "'>下一页</button>").click(function () {
	                        	opts.param.index = index + 1;
	                        	$.pangination(opts);
	                            return false;
	                        });
	                        $pageFoot.append(next);
	                    }

	                    //最后一页
	                    if (index == end) {
	                        $pageFoot.append("<button class='disabled btn'>尾页&nbsp;&gt;</button>");

	                    }
	                    else {
	                        var last = $("<button last='" + pageCount + "' class='btn'>尾页&nbsp;&gt;</button>").click(function () {
	                        	opts.param.index = pageCount;
	                        	$.pangination(opts);
	                            return false;
	                        });
	                        $pageFoot.append(last);
	                    }
					}
			}
})(jQuery);

